package model.location;

import android.util.Log;
import model.Coordinate;
import model.Dimension;
import model.OnScreen;
import model.Snippet;

public class ScreenLocationCalculator 
{
	private static float scaleX;
//	private static float scaleY;
	
	public static Snippet calculateScreenLocation(Snippet snippet)
	{
		Coordinate screenCoord = convertCoordinate((WorldCoordinate) snippet.getWorldArea().getCoordinate());
		Dimension screenDimension = convertDimension(snippet.getWorldArea().getDimension());
		
		snippet.setScreenArea(new ScreenArea(screenCoord, screenDimension));
		return snippet;
	}
	
	public static void calculateScales()
	{
		calculateScaleX();
		calculateScaleY();
	}
	
	private static void calculateScaleX()
	{
		float width = OnScreen.getInstance().getLowerRight().getX() -  OnScreen.getInstance().getUpperLeft().getX();
		
		scaleX = width / (float) OnScreen.getInstance().getPreviewDimension().getWidth();
	}
	
	private static void calculateScaleY()
	{
		//double height = OnScreen.getInstance().getUpperLeft().getY() - OnScreen.getInstance().getLowerRight().getY();
		
		//scaleY = height / (double) OnScreen.getInstance().getScreenDimension().getHeight();
	}
	
	private static Dimension convertDimension(Dimension dimension)
	{
		float width = (dimension.getWidth() / scaleX);
		float height = (dimension.getHeight() / scaleX);
		Dimension dim = new Dimension(width, height);
		
		return dim;
	}
	
	private static Coordinate convertCoordinate(WorldCoordinate coordinate)
	{
		float xOffset = coordinate.getX() - OnScreen.getInstance().getUpperLeft().getX();
		float yOffset = OnScreen.getInstance().getUpperLeft().getY() - coordinate.getY();
		
		float xCorrection = OnScreen.getInstance().getXCorrection();
		float yCorrection = OnScreen.getInstance().getYCorrection();
		
		float xPixelOffset = (xOffset / scaleX);
		float yPixelOffset = (yOffset / scaleX);
		
//		Log.i("LOC", String.format("before(%f:%f) - corr:(%f:%f)", xPixelOffset, yPixelOffset, xCorrection, yCorrection));
//		
//		xPixelOffset *= xCorrection;
//		yPixelOffset *= yCorrection;
//		
//		Log.i("LOC", String.format("after(%f:%f)", xPixelOffset, yPixelOffset));
		
		Coordinate coordinate2d = new Coordinate(xPixelOffset, yPixelOffset);
		
		return coordinate2d;
	}
}
